package com.shxt.hotel.background;

import com.shxt.hotel.util.*;
import com.shxt.hotel.conf.*;
import java.util.Map;
import java.util.List;
import java.util.HashMap;

public class Consume{
    
    DBManager dbm;
    
    public Consume()
    throws Exception{
        try{
            dbm = new DBManager(Config.DB_NAME, Config.DB_USERNAME, Config.DB_PASSWORD);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int userConsumeJSON(String objJSON, int uid, int discount)
    throws Exception{
        try{
            int row = 0;
            
            List<Map<String, Object>> objList = JSONParser.JSONToList(objJSON);
            for(Map<String, Object> map : objList){
                map.put("cid", map.get("id"));
                map.remove("id");
                map.put("uid", uid);
                map.put("create_date", Time.getDate());
                
                Map<String, Object> consumeTmp = this.getConsume(Integer.parseInt((String)map.get("cid")));
                float deposit = (float)consumeTmp.get("price") * Integer.parseInt((String)map.get("num")) * (100.0f - discount) / 100.0f;
                map.put("deposit", deposit);
                
                row += dbm.insert("consume_info", map);
            }
            
            return row;
            
        }catch(Exception e){
            throw e;
        }
    }
    
    public List<Map<String, Object>> getConsumes()
    throws Exception{
        try{
            String sql = "select * from consume";
            return dbm.executeQuery(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public Map<String, Object> getConsume(int id)
    throws Exception{
        try{
            String sql = "select * from consume where id = " + id;
            return dbm.executeQuery(sql).get(0);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int orderConsumeInfo(int id)
    throws Exception{
        try{
            String sql = "update consume_info set is_order = 1 where id = " + id;
            return dbm.executeUpdate(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public List<Map<String, Object>> getConsumeInfo()
    throws Exception{
        try{
            String sql = "select * from consume_info where is_order = 0";
            return dbm.executeQuery(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public List<Map<String, Object>> getConsumeInfoListByUserId(int uid)
    throws Exception{
        try{
            String sql = "select * from consume_info where uid = " + uid;
            return dbm.executeQuery(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int addConsume(Map<String, String[]> paraMap)
    throws Exception{
        try{
            Map<String, Object> newParaMap = dbm.packageParaMap(paraMap);
            return dbm.insert("consume", newParaMap);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int editConsume(Map<String, String[]> paraMap)
    throws Exception{
        try{
            Map<String, Object> newParaMap = dbm.packageParaMap(paraMap);
            Map<String, Object> whereMap = new HashMap<String, Object>();
            whereMap.put("id", newParaMap.get("id"));
            newParaMap.remove("id");
            
            return dbm.update("consume", newParaMap, whereMap);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int deleteConsume(int cid)
    throws Exception{
        try{
            String sql = "delete from consume where id = " + cid;
            return dbm.executeUpdate(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public List<Map<String, Object>> getCategories()
    throws Exception{
        try{
            String sql = "select * from consume_category";
            return dbm.executeQuery(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public Map<String, Object> getConsumeCategoryByCategoryId(int cid)
    throws Exception{
        try{
            String sql = "select * from consume_category where id = " + cid;
            return dbm.executeQuery(sql).get(0);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int addCategory(Map<String, String[]> paraMap)
    throws Exception{
        try{
            Map<String, Object> newParaMap = dbm.packageParaMap(paraMap);
            return dbm.insert("consume_category", newParaMap);
        }catch(Exception e){
            throw e;
        }
    }
    
    public void close()
    throws Exception{
        try{
            dbm.close();
        }catch(Exception e){
            throw e;
        }
    }
    
}